<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="item in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: activeIndex === item.roleId }"
        @click="changeRole(item.roleId)"
      >
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/addRole/${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="permission">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="tree"
                :data="item.children"
                node-key="id"
                default-expand-all
                :expand-on-click-node="false"
                show-checkbox
                :props="defaultProps"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="memberTotal" name="member">
          <div class="user-wrapper">
            <el-table :data="roleUserList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
            <el-pagination
              layout="total, prev, pager, next, sizes"
              :page-size.sync="pageSize"
              :total="total"
              :page-sizes="[10, 20, 50, 100]"
              background
              :current-page.sync="page"
              @current-change="getRoleUserList"
              @size-change="handleSizeChange"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { delRoleAPI, getAllPermissionAPI, getRoleListAPI, getRolePermissionAPI, getRoleUserListAPI } from '@/api/system-role'

export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: null,
      treeList: [],
      defaultProps: {
        label: 'title'
      },
      page: 1,
      pageSize: 10,
      total: 0,
      activeName: 'permission',
      roleUserList: []
    }
  },
  computed: {
    memberTotal() {
      return `成员列表(${this.total})`
    }
  },
  async created() {
    await this.getRoleList()
    await this.getAllPermission()
    await this.changeRole(this.roleList[0].roleId)
    // this.getRoleUserList()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI()
      // console.log('角色列表', res)
      this.roleList = res.data
    },
    // 递归函数 --- 设置所有树节点的禁用状态
    addDisabled(list) {
      list.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
      return list
    },
    // 获取所有的功能权限
    async getAllPermission() {
      const res = await getAllPermissionAPI()
      // console.log('所有的功能权限', res)
      this.treeList = res.data
      this.treeList = this.addDisabled(this.treeList)
    },
    async changeRole(id) {
      // 1. 切换高亮显示
      this.activeIndex = id
      // 2. 发送请求
      const roleId = this.roleList[this.activeIndex]?.roleId
      if (roleId) {
        const res = await getRolePermissionAPI(roleId)
        // console.log('角色权限点详情', res)
        const perms = res.data.perms
        const trees = this.$refs.tree
        trees.forEach((item, index) => {
          item.setCheckedKeys(perms[index])
        })
      }
      this.getRoleUserList()
    },
    // 获取角色关联的成员列表你
    async getRoleUserList() {
      const res = await getRoleUserListAPI({
        roleId: this.activeIndex,
        pageSize: this.pageSize,
        page: this.page
      })
      // console.log('成员列表', res)
      this.roleUserList = res.data.rows
      this.total = res.data.total
    },
    // 列表展示条目发生变化
    handleSizeChange() {
      this.page = 1
      this.getRoleUserList()
    },
    // 删除角色
    async delRole(id) {
      await this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
      await delRoleAPI(id)
      this.$message.success('删除成功')
      this.getRoleList()
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
